#include "qdatetimeproperty.h"

#include <QDateTimeEdit>

QDateTimeProperty::QDateTimeProperty(SHAREDLIB::QAbstractProperty *parent):
    SHAREDLIB::QAbstractProperty(parent)
{
    connect(this,SIGNAL(showValueChanged()),this,SLOT(value_changed()));
}

void QDateTimeProperty::valueXML(QXmlStreamWriter *w)
{
    QTime ti=getValue().toDateTime().time();
    QDate dt=getValue().toDateTime().date();
    w->writeAttribute("year",QString::number(dt.year()));
    w->writeAttribute("month",QString::number(dt.month()));
    w->writeAttribute("day",QString::number(dt.day()));
    w->writeAttribute("hour",QString::number(ti.hour()));
    w->writeAttribute("minute",QString::number(ti.minute()));
    w->writeAttribute("second",QString::number(ti.second()));
}

void QDateTimeProperty::parserXML(QXmlStreamReader *r)
{
    QDateTime dt;

    int year,month,day;
    year=2000;
    month=1;
    day=1;
    int hour,minute,second;
    hour=0;
    minute=0;
    second=0;

    foreach(const QXmlStreamAttribute &attribute,r->attributes())
    {
        if(attribute.name().toString()=="year")
        {
            year=attribute.value().toString().toInt();
        }
        else if(attribute.name().toString()=="month")
        {
            month=attribute.value().toString().toInt();
        }
        else if(attribute.name().toString()=="day")
        {
            day=attribute.value().toString().toInt();
        }
        else if(attribute.name().toString()=="hour")
        {
            hour=attribute.value().toString().toInt();
        }
        else if(attribute.name().toString()=="minute")
        {
            minute=attribute.value().toString().toInt();
        }
        else if(attribute.name().toString()=="second")
        {
            second=attribute.value().toString().toInt();
        }
    }

    for(bool finished=false;!finished && !r->hasError();)
    {
        switch(r->readNext())
        {
        case QXmlStreamReader::StartElement:
            break;
        case QXmlStreamReader::EndElement:
            finished=true;
            break;
        default:
            break;
        }
    }

    dt.setDate(QDate(year,month,day));
    dt.setTime(QTime(hour,minute,second));
    setValue(dt);
}

QWidget* QDateTimeProperty::getEditor()
{
    if(attribute("editabled").toBool())
    {
        QDateTimeEdit *ti=new QDateTimeEdit;
        connect(ti,SIGNAL(dateTimeChanged(QDateTime)),this,SLOT(editor(QDateTime)));
        m_editor=ti;
    }
    return QAbstractProperty::getEditor();
}

void QDateTimeProperty::editor(const QDateTime &datetime)
{
    QDateTime v=getValue().toDateTime();
    if(v!=datetime)
    {
        setModified(true);
        setSignalValue(datetime);
    }
}

QString QDateTimeProperty::valueText()
{
    QDate dt=getValue().toDateTime().date();
    QTime ti=getValue().toDateTime().time();
    return QString("%1-%2-%3 %4:%5:%6").arg(dt.year(),4).arg(dt.month(),2).arg(dt.day(),2)
            .arg(ti.hour(),2).arg(ti.minute(),2).arg(ti.second(),2);
}

void QDateTimeProperty::value_changed()
{
    if(m_editor!=NULL)
    {
        ((QDateTimeEdit*)m_editor)->setDateTime(m_value.toDateTime());
    }
}
